﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Homework.Properties;

namespace Homework
{

    /// <summary>
    /// 玩家飞机
    /// </summary>
    class PlaneHero
    {
        //获得游戏对象的矩形
        public Rectangle rectangle
        {
            get;
            set;
        }
        //说明切换图片的布尔值
        private bool picture = true;
        //说明并初始化切换图片的时间
        private DateTime picturechanged = System.DateTime.Now.AddHours(-1);

        //玩家飞机
        public PlaneHero(Rectangle rec, int PlaneHerolifenow)
        {
            //给矩形赋值
            this.rectangle = rec;
        }

        //定义玩家飞机的生命值
        public int PlaneHerolifenow = 15;
        //定义Step的值
        public int Step = 10;
        //说明闪烁的布尔值
        public bool HeroChanged;

        /// <summary>
        /// 绘制玩家飞机函数
        /// </summary>
        /// <param name="graphics"></param>
        public void Draw(Graphics graphics)
        {
            if (System.DateTime.Now > picturechanged)
            {
                if (HeroChanged == true)
                {
                    if (picture)
                    {
                        graphics.DrawImage(Properties.Resources.PlaneHero, rectangle);
                        picture = false;
                    }
                    else
                    {
                        graphics.DrawImage(Properties.Resources.PlaneHero2, rectangle);
                        picture = true;
                    }
                    picturechanged = System.DateTime.Now.AddSeconds(0.1);
                    HeroChanged = false;
                }
                else
                {
                    graphics.DrawImage(Properties.Resources.PlaneHero, rectangle);
                }
            }           
        }

        /// <summary>
        /// 处理键盘
        /// </summary>
        public void KeyDown(String key)
        {
            //定义按键的作用效果
            switch (key)
            {
                case "A":
                    this.rectangle = new Rectangle(this.rectangle.X - Step, this.rectangle.Y, this.rectangle.Width, this.rectangle.Height);
                    break;
                case "D":
                    this.rectangle = new Rectangle(this.rectangle.X + Step, this.rectangle.Y, this.rectangle.Width, this.rectangle.Height);
                    break;
                case "W":
                    this.rectangle = new Rectangle(this.rectangle.X, this.rectangle.Y - Step, this.rectangle.Width, this.rectangle.Height);
                    break;
                case "S":
                    this.rectangle = new Rectangle(this.rectangle.X, this.rectangle.Y + Step, this.rectangle.Width, this.rectangle.Height);
                    break;
            }
        }
        /// <summary>
        /// 处理移动
        /// </summary>
        public void Move()
        {
            //移动完成后 保证玩家飞机不超过游戏区域
            if (this.rectangle.X <= 0)
            {
                this.rectangle = new Rectangle(0,this.rectangle.Y, rectangle.Width, rectangle.Height);
            }
            if (this.rectangle.X >= 663)
            {
                this.rectangle = new Rectangle(663, this.rectangle.Y, rectangle.Width, rectangle.Height);
            }
            if (this.rectangle.Y <= 0)
            {
                this.rectangle = new Rectangle(this.rectangle.X,0, rectangle.Width, rectangle.Height);
            }
            if (this.rectangle.Y >= 540)
            {
                this.rectangle = new Rectangle(this.rectangle.X, 540, rectangle.Width, rectangle.Height);
            }
        }
    }
}
